package com.istock.union.user.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Repository;

import com.istock.base.spring.dynamicLoad.PropertyStaticLoader;
import com.istock.union.user.model.LoginforInfo;
import com.istock.union.user.model.OperLogInfo;
import com.istock.union.user.model.UserOnlineInfo;
import com.istock.union.user.service.AschTaskService;
import com.istock.union.user.service.LoginforInfoService;
import com.istock.union.user.service.OperLogInfoService;
import com.istock.union.user.service.UserOnlineInfoService;

@Repository
public class AschTaskServiceImpl implements AschTaskService{

	@Resource(name="AschTaskExecutor")
	private ThreadPoolTaskExecutor executor;
	
	@Autowired
	private LoginforInfoService loginforService;
	
	@Autowired
	private OperLogInfoService operLogInfoService;
	
	@Autowired
	private UserOnlineInfoService userOnlineInfoService;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	public void userOnlineRecord(final UserOnlineInfo userOnlineInfo){
		executor.execute(new Runnable(){
			@Override
			public void run() {
				userOnlineInfo.setStartTimestamp(new Date());
				userOnlineInfo.setExpireTime(DateUtils.addSeconds(new Date(), Integer.parseInt(PropertyStaticLoader.getInstance().findProperty("sso.token.expireTime", "1800"))));
				userOnlineInfoService.insertUserOnline(userOnlineInfo);
				
				/*LoginforInfo loginLog = new LoginforInfo();
				loginLog.setBrowser(userOnlineInfo.getBrowser());
				loginLog.setIpaddr(userOnlineInfo.getIpaddr());
				loginLog.setLoginLocation(userOnlineInfo.getLoginLocation());
				loginLog.setLoginName(userOnlineInfo.getLoginName());
				loginLog.setOperTime(new Date());
				loginLog.setTenantCode(userOnlineInfo.getTenantCode());
				loginLog.setOs(userOnlineInfo.getOs());
				loginLog.setLogType(Enums.getInstance().getEnumsValueByCode("LOG_TYPE", "LOG_TYPE_IN"));*/
				
				
			}
		});
	}
	
	public void userOnlineRecordUpdate(final String sessionId , final String deptCode) {
		executor.execute(new Runnable() {
			public void run() {
				UserOnlineInfo userOnlineInfo = new UserOnlineInfo();
				if(StringUtils.isNotBlank(deptCode)) {
					userOnlineInfo.setDeptCode(deptCode);
				}
				userOnlineInfo.setLastAccessTime(new Date());
				userOnlineInfo.setExpireTime(DateUtils.addSeconds(new Date(), Integer.parseInt(PropertyStaticLoader.getInstance().findProperty("sso.token.expireTime", "1800"))));
				userOnlineInfoService.updateUserOnline(sessionId, userOnlineInfo);
			}
		});
	}
	
	public void userOnlineRemove(final String[] tokenArray) {
		if (executor != null) {
			executor.submit(new Runnable() {
				@Override
				public void run() {
					/*List<LoginforInfo> loginInfoList = loginforService.findLoginListByToken(Arrays.asList(tokenArray));
					if(loginInfoList != null && loginInfoList.size()>0) {
						List<LoginforInfo> newList = new ArrayList<LoginforInfo>();
						for(LoginforInfo info : loginInfoList) {
							LoginforInfo newInfo = new LoginforInfo();
							BeanUtils.copyProperties(info, newInfo);
							newInfo.setLogType(Enums.getInstance().getEnumsValueByCode("LOG_TYPE", "LOG_TYPE_OUT"));
							newInfo.setOperTime(new Date());
							newList.add(newInfo);
						}
						loginforService.insertLoginBatch(newList);
					}*/
					
					try {
						userOnlineInfoService.remveUserOnlineInfo(tokenArray);
					} catch (Exception e) {
						logger.info("remveUserOnlineInfo exception ", e);
					}

				}

			});
		}
	}
	
	/**
	 * 记录操作日志
	 */
	public void operateRecord(final OperLogInfo operLog){
		executor.execute(new Runnable(){
			@Override
			public void run() {
				operLogInfoService.insertOperLog(operLog);
			}
		});
	}

	@Override
	public void loginInfoRecord(final LoginforInfo loginInfo) {
		executor.execute(new Runnable(){
			@Override
			public void run() {
				loginforService.insertLoginfor(loginInfo);
			}
		});
	}
}
